//
//  AddViewController.m
//  17-UI-AddressBook
//
//  Created by Snow on 16/8/6.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "AddViewController.h"
#import "Model.h"
#import "ImageViewOfVC2nd.h"
@interface AddViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UITextField *textOfName;
@property (nonatomic, strong) UITextField *textOfAge;
@property (nonatomic, strong) UITextField *textOfGender;
@property (nonatomic, strong) UITextField *textOfPhoneNumber;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) NSMutableDictionary *dictionary;
@property (nonatomic, strong) Model *model;

@property (nonatomic, strong) ImageViewOfVC2nd *imageOfView;
@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dictionary = [NSMutableDictionary dictionary];
    self.view.backgroundColor = [UIColor whiteColor];
    _model = [[Model alloc] init];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(80, 400, 200, 30);
    button.backgroundColor = [UIColor greenColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"添加" forState:UIControlStateNormal];
    
    UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 150, 30)];
    UILabel *labelOfGender = [[UILabel alloc] initWithFrame:CGRectMake(10, 190, 150, 30)];
    UILabel *labelOfPhoneNumber = [[UILabel alloc] initWithFrame:CGRectMake(10, 230, 150, 30)];
    UILabel *labelOfAge = [[UILabel alloc] initWithFrame:CGRectMake(10, 270, 150, 30)];
    labelOfName.text = @"Name:";
    labelOfAge.text = @"Age:";
    labelOfGender.text = @"gender:";
    labelOfPhoneNumber.text = @"phoneNumber:";
    
    [self.view addSubview:labelOfPhoneNumber];
    [self.view addSubview:labelOfAge];
    [self.view addSubview:labelOfGender];
    [self.view addSubview:labelOfName];
    _image = [[UIImageView alloc] init];
    _image.frame = CGRectMake(137.5, 0, 100, 100);
    _image.image = [UIImage imageNamed:@"u=1563663233,4023384541&fm=23&gp=0"];
    _image.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    self.textOfName = [[UITextField alloc] initWithFrame:CGRectMake(170, 150, 200, 30)];
    self.textOfAge = [[UITextField alloc] initWithFrame:CGRectMake(170, 270, 200, 30)];
    self.textOfGender = [[UITextField alloc] initWithFrame:CGRectMake(170, 190, 200, 30)];
    self.textOfPhoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(170, 230, 200, 30)];
    self.textOfName.placeholder = @"请输入姓名";
    self.textOfAge.placeholder = @"请输入年龄";
    self.textOfGender.placeholder = @"请输入性别";
    self.textOfPhoneNumber.placeholder = @"请输入电话号码";
    [self.view addSubview:_image];
    [self.view addSubview:self.textOfPhoneNumber];
    [self.view addSubview:self.textOfGender];
    [self.view addSubview:self.textOfAge];
    [self.view addSubview:self.textOfName];
     [_image addGestureRecognizer:tap];
}
- (void)handleTap:(UITapGestureRecognizer *)tap
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
- (void)handleButton:(UIButton *)button
{
    if (self.textOfName.text != nil && self.textOfAge.text != nil && self.textOfGender.text != nil && self.textOfPhoneNumber.text != nil) {
        [self.dictionary setObject:self.textOfName.text forKey:@"name"];
        [self.dictionary setObject:self.textOfGender.text forKey:@"gender"];
        [self.dictionary setObject:self.textOfPhoneNumber.text forKey:@"phoneNumber"];
        [self.dictionary setObject:_image.image forKey:@"image"];
        [self.dictionary setObject:self.textOfAge.text forKey:@"age"];
    }
    [_model setValuesForKeysWithDictionary:_dictionary];
    if (self.delegate && [self.delegate respondsToSelector:@selector(addContact:)]) {
        [self.delegate addContact:_model];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - pickerDelegate 协议方法.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    // 获取图片
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.imageOfView.image = image;
    // 退出 picker 界面
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo
{
    _image.image = image;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
